package com.example.scalewidth;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.piccut.R;

public class ScaleWidth extends View {
    private Path mPath;
    private Paint mPaint;
    private int mWidth;
    private int mHeight;
    private float mWidthWeigth = 1f;
    private Bitmap mBmp;

    public ScaleWidth(Context context) {
        super(context);
        init();
    }

    public ScaleWidth(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ScaleWidth(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init() {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setColor(Color.RED);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(100);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mBmp = BitmapFactory.decodeResource(getResources(), R.drawable.copper_inst_text);
        setOnClickListener(new OnClickListener() {
            private boolean mReverse = false;
            @Override
            public void onClick(View v) {
                ValueAnimator valueAnimator = ValueAnimator.ofFloat(mReverse ? 0f : 1f, mReverse ? 1f : 0f);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        mWidthWeigth = (float) animation.getAnimatedValue();
                        invalidate();
                    }
                });
                valueAnimator.setDuration(1000);
                valueAnimator.start();
                mReverse = !mReverse;
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int w = MeasureSpec.getSize(widthMeasureSpec);
        int h = MeasureSpec.getSize(heightMeasureSpec);
        if (w != mWidth || h != mHeight) { //已经onMeasuer过一次，除非界面大小改动否则不重新初始化view
            mWidth = w;
            mHeight = h;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPath = new Path();
        int refreshVal = (int) (mWidth * mWidthWeigth - mPaint.getStrokeWidth() / 2);
        mPath.moveTo(mPaint.getStrokeWidth() / 2, mHeight / 2);
        mPath.lineTo(refreshVal > 0 ? refreshVal : 0, mHeight / 2);
        if (refreshVal > mPaint.getStrokeWidth()) {
            canvas.drawPath(mPath, mPaint);
            PathMeasure pathMeasure = new PathMeasure(mPath, false);
            canvas.save();
            canvas.clipRect(0, 0, pathMeasure.getLength(), mHeight);
            canvas.drawBitmap(mBmp, new Rect(0, 0, mBmp.getWidth(), mBmp.getHeight()),
                    new Rect(mWidth / 2 - mBmp.getWidth() / 2, mHeight / 2 - mBmp.getHeight() / 2,
                            mWidth / 2 - mBmp.getWidth() / 2 + mBmp.getWidth(), mHeight / 2 - mBmp.getHeight() / 2 + mBmp.getHeight()), null);
            canvas.restore();
        }
        super.onDraw(canvas);
    }
}
